package com.echo.week;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author echo
 * @Description  CyclicBarrier  循环栅栏 案例---七龙珠召唤神龙
 * @Date 2023/10/21.
 * @Version 1.0
 */
public class DemoCyclicBarrier {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("=====召唤神龙！");
        });

        for (int i = 1; i <=14 ; i++) {
            final int tempInt = i;
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName()+"\t收集到第"+tempInt+"颗龙珠！");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName()+"\t第"+tempInt+"颗龙珠丢失了！");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            },"Thread-"+String.valueOf(i)).start();
        }
    }
}
